//
//  RYQuerenPeisongCell.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/13.
//  Copyright © 2021 叁拾叁. All rights reserved.
//



import UIKit

class RYQuerenPeisongCell: SYBaseCell , UITableViewDelegate, UITableViewDataSource{
    
    var tableView: UITableView!
    var contents:[String] = ["", ""]
    
    var bgView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = .white
        v.layer.cornerRadius = 5
        v.layer.masksToBounds = true
        return v
    }()
    
    let nameLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x1D1D1D)
        v.font = 16.autoBoldFontSize()
        v.text = "配送信息"
        return v
    }()
    
    override func initViews() {
        
        setTableView()
        self.backgroundColor = Constant.bgViewColor
        
        addSubview(bgView)
        
        bgView.addSubview(nameLb)
        bgView.addSubview(tableView)
        
        bgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(15.autoWidth())
            make.bottom.equalToSuperview()
            make.left.equalToSuperview().offset(15.autoWidth())
            make.right.equalToSuperview().offset(-15.autoWidth())
        }
        nameLb.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(15.autoWidth())
            make.left.equalToSuperview().offset(15.autoWidth())
            make.right.equalToSuperview().offset(-15.autoWidth())
            make.height.equalTo(16.autoWidth())
        }
        tableView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(30.autoWidth())
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(84.autoWidth())
            make.bottom.equalToSuperview()
        }
    }
    
    func setTableView() {
        
        //创建表格视图
        self.tableView = UITableView(frame: CGRect(x: 0, y: NAV_HEIGHT, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-NAV_HEIGHT-BOTTOM_SAFE_HEIGHT), style:.plain)
        self.tableView.separatorStyle = .none
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.isScrollEnabled = false
        self.tableView!.register(RYQuerenPeisongSubCell.self, forCellReuseIdentifier: "Cell")
        self.contentView.addSubview(self.tableView!)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: RYQuerenPeisongSubCell? = tableView.dequeueReusableCell(withIdentifier: "Cell") as? RYQuerenPeisongSubCell
        if indexPath.row == 0 {
            cell?.title.text = "配送方式"
            if contents.first?.isEmpty == false {
                cell?.content.text = contents[indexPath.row]
                cell?.content.textColor = UIColor(hex: 0x333333)
            } else {
                cell?.content.text = "快递运输"
            }
        } else {
            cell?.title.text = "配送周期"
            if  contents.last?.isEmpty == false  {
                cell?.content.text = contents[indexPath.row]
                cell?.content.textColor = UIColor(hex: 0x333333)
            } else {
                cell?.content.text = "120天（至少配送一次）"
            }
        }
        
        cell?.selectionStyle = .none
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = (self.currentVC as! RYQuerenDIngdanViewController)
        if indexPath.row == 0 {
            
            var dics: [[String: String]] = []
            for (_ ,value) in vc.kuaidiModels.enumerated() {
                var dic: [String: String] = [:]
                dic["name"] = value.dictionaryName
                dic["id"] = value.id
                dics.append(dic)
            }
            self.setupSheet(dics,  vc.view, "配送方式选择", 0)
        } else {
            var dics: [[String: String]] = []
            for (_ ,value) in vc.peisongZhouqiModels.enumerated() {
                var dic: [String: String] = [:]
                dic["name"] = value.dictionaryName
                dic["id"] = value.id
                dics.append(dic)
            }
            self.setupSheet(dics, vc.view, "配送方式周期", 1)
        }
    }
    
    func setupSheet(_ dics: [Any], _ showView: UIView, _ biaoti: String, _ index: Int) {
        
        let dialog:WMZDialog = Dialog()
        dialog
            .wTypeSet()(DialogTypeSheet)
            .wTitleSet()(biaoti)
            .wDataSet()(dics.map{ return ($0 as! [String: String])["name"] })
            .wListDefaultValueSet()([(dics[0] as! [String: String])["name"] ?? ""])
            .wTapViewSet()(showView)
            .wOKColorSet()(UIColor(hex: 0x367FF6))
            .wEventFinishSet()({[weak self](anyID:Any?,path:IndexPath?,type:DialogType) in
                print("选择",anyID as Any);
                let currentvc = (self?.currentVC as! RYQuerenDIngdanViewController)
                if let `self` = self {
                    self.contents[index] = anyID as? String ?? ""
                    self.tableView.reloadData()
                    let dics:[[String: String]] = dics.filter({  ($0 as! [String: String])["name"] == (anyID as! String) }) as! [[String: String]]
                    if index == 0 {
                        currentvc.params["deliveryWay"] = dics[0]["id"] ?? ""
                    } else {
                        currentvc.params["deliveryCycle"] = dics[0]["id"] ?? ""
                    }
                }
                
            })
        _ = dialog.wStart()
        
        
        //            kuaidiChoice.setupSheet(titles, view: self.view, color: UIColor(hex: 0x367FF6))
    }
}
